//
//  CommentToolBar.m
//  MeiXiu
//
//  Created by Yuan on 12-10-31.
//
//

#import "CommentToolBar.h"

#define kTOOLBAR_HEIGHT 44.0

@implementation CommentToolBar

@synthesize delegate,text;

- (id)initWithFrame:(CGRect)frame delegate:(id)delegate_
{    
    self = [super initWithFrame:CGRectMake(frame.origin.x,
										   frame.origin.y,
										   frame.size.width,
										   kTOOLBAR_HEIGHT)];
    if (self)
    {
        self.delegate = delegate_;
        
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"proshow_bottombar.png"]];
        
        CGRect txtRect = CGRectMake(10.0f,7.0,frame.size.width-90.0f,31);
		txtField=[[UITextField alloc]initWithFrame:txtRect];
		txtField.borderStyle=UITextBorderStyleRoundedRect;
		txtField.clearButtonMode=UITextFieldViewModeWhileEditing;
		txtField.delegate=self;
		txtField.returnKeyType=UIReturnKeyDone;
		txtField.placeholder=@"输入评论";
		[self addSubview:txtField];
		
		UIImage *theBtnImg=[UIImage  imageNamed:@"proshow_send.png"];
		doneBtn=[UIButton buttonWithType:UIButtonTypeCustom];
		[doneBtn setBackgroundImage:theBtnImg forState:UIControlStateNormal];
		doneBtn.titleLabel.font=[UIFont fontWithName:@"msyh" size:14.0f];
		[doneBtn addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat offset_x = txtField.frame.origin.x+txtField.frame.size.width+10.0f;
		doneBtn.frame=CGRectMake(offset_x,txtField.frame.origin.y,60.0f, 31.0f);
		[self addSubview:doneBtn];
		
		self.text=txtField.text;
    }
    return self;
}

-(void)doneAction:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	NSString *_text=txtField.text;
	
	if (delegate&&[delegate respondsToSelector:@selector(toolBarButtonDone:toolBarView:text:)])
    {
        [delegate toolBarButtonDone:btn toolBarView:self text:_text];
        txtField.text = @"";
	}
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(toolBarDidBegainEditing:)])
    {
        [delegate toolBarDidBegainEditing:self];
	}
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(toolBarDidEndEditing:)]) {
		[delegate toolBarDidEndEditing:self];
	}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(toolBarShoulReturn:)]) {
		[delegate toolBarShoulReturn:self];
	}
	return YES;
}

-(BOOL)resignFirstResponder
{
	return [txtField resignFirstResponder];
}

-(BOOL)becomeFirstResponder
{
	return [txtField becomeFirstResponder];
}

-(void)setText:(NSString *)_txt
{
	txtField.text=_txt;
	if (text) {
		[text release];
	}
	text=[_txt retain];
}

-(NSString *)text
{
	return txtField.text;
}

- (void)dealloc
{
	[txtField release];
	delegate=nil;
	[text release];
    [super dealloc];
}


@end
